home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / GDEVSVGA.C < prev    next >
C/C++ Source or Header  |  1992-02-16  |  20KB  |  668 lines

  1. /* Copyright (C) 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gdevsvga.c */
  21. /* SuperVGA display drivers for Ghostscript */
  22. #include "dos_.h"
  23. typedef union REGS registers;
  24. #include "memory_.h"
  25. #include "gs.h"
  26. #include "gxdevice.h"
  27.  
  28. #ifndef USE_ASM
  29. #  define USE_ASM 0            /* don't use assembly language */
  30. #endif
  31.  
  32. /* Define the short (integer) version of "transparent" color. */
  33. /* ****** Depends on gx_no_color_index being all 1's. ******/
  34. #define no_color ((int)gx_no_color_index)
  35.  
  36. /* Procedures */
  37.  
  38.     /* See gxdevice.h for the definitions of the procedures. */
  39.  
  40. private dev_proc_close_device(svga_close);
  41. private dev_proc_map_rgb_color(svga_map_rgb_color);
  42. private dev_proc_map_color_rgb(svga_map_color_rgb);
  43. private dev_proc_fill_rectangle(svga_fill_rectangle);
  44. private dev_proc_copy_mono(svga_copy_mono);
  45. private dev_proc_copy_color(svga_copy_color);
  46. private dev_proc_get_bits(svga_get_bits);
  47.  
  48. /* Type for frame buffer pointers. */
  49. /*** Intimately tied to the 80x86 (x<2) addressing architecture. ***/
  50. typedef byte far *fb_ptr;
  51.  
  52. /* The device descriptor */
  53. typedef struct gx_device_svga_s gx_device_svga;
  54. struct gx_device_svga_s {
  55.     gx_device_common;
  56.     int (*get_mode)(P0());
  57.     void (*set_mode)(P1(int));
  58.     void (*set_page)(P3(gx_device_svga *, int, int));
  59.     uint raster;            /* frame buffer bytes per line */
  60.     int page;            /* current page */
  61.     int wnum_read, wnum_write;    /* window #s for read vs. write */
  62.     /* Following are device-specific. */
  63.     union {
  64.       struct {
  65.         void (*bios_set_page)(P2(int, int));    /* set-page function */
  66.       } vesa;
  67.       struct {
  68.         int select_reg;            /* page-select register */
  69.       } atiw;
  70.     } info;
  71. };
  72.  
  73. /* The color map for dynamically assignable colors. */
  74. #define first_color_index 64
  75. private int next_color_index;
  76. private ushort dynamic_colors[256 - first_color_index];
  77.  
  78. /* Macro for casting gx_device argument */
  79. #define fb_dev ((gx_device_svga *)dev)
  80.  
  81. /* Procedure records */
  82. #define svga_procs(open) {\
  83.     open, gx_default_get_initial_matrix,\
  84.     gx_default_sync_output, gx_default_output_page, svga_close,\
  85.     svga_map_rgb_color, svga_map_color_rgb,\
  86.     svga_fill_rectangle, gx_default_tile_rectangle,\
  87.     svga_copy_mono, svga_copy_color, gx_default_draw_line,\
  88.     svga_get_bits, gx_default_get_props, gx_default_put_props\
  89. }
  90.  
  91. /* The initial parameters map an appropriate fraction of */
  92. /* the screen to an 8.5" x 11" coordinate space. */
  93. /* This may or may not be what is desired! */
  94. #define svga_device(procs, name, get_mode, set_mode, set_page) {\
  95.     sizeof(gx_device_svga),\
  96.     &procs,\
  97.     name,\
  98.     640, 480,        /* screen size */\
  99.     480 / 11.0, 480 / 11.0,    /* resolution */\
  100.     no_margins,\
  101.     dci_color(8, 31, 4),\
  102.     0,            /* not opened yet */\
  103.     get_mode, set_mode, set_page\
  104.    }
  105.  
  106. /* Save the controller mode */
  107. private int svga_save_mode = -1;
  108.  
  109. /* Macro for validating rectangle parameters x and w. */
  110. /* set_pixel_ptr implicitly validates y and h. */
  111. #define validate_rect()\
  112.   if ( w <= 0 ) return 0;\
  113.   if ( x < 0 || x + w > dev->width ) return -1
  114.  
  115. /* ------ Internal routines ------ */
  116.  
  117. #define regen 0xa000
  118.  
  119. /* Construct a pointer for writing a pixel. */
  120. /* Assume 64K pages, 64K granularity. */
  121. #define set_pixel_ptr(ptr, fbdev, x, y, wnum)\
  122. {    ulong index = (ulong)(y) * fbdev->raster + (uint)(x);\
  123.     if ( (uint)(index >> 16) != fbdev->page )\
  124.        {    if ( y < 0 || y >= fbdev->height ) return -1;\
  125.         (*fbdev->set_page)(fbdev, (fbdev->page = index >> 16), wnum);\
  126.        }\
  127.     ptr = (fb_ptr)MK_PTR(regen, (uint)index);\
  128. }
  129. #define set_pixel_write_ptr(ptr, fbdev, x, y)\
  130.   set_pixel_ptr(ptr, fbdev, x, y, fbdev->wnum_write)
  131. #define set_pixel_read_ptr(ptr, fbdev, x, y)\
  132.   set_pixel_ptr(ptr, fbdev, x, y, fbdev->wnum_read)
  133.  
  134. /* Table structure for looking up graphics modes. */
  135. typedef struct {
  136.     int width, height;        /* "key" */
  137.     int mode;            /* "value" */
  138. } mode_info;
  139.  
  140. /* Find the graphics mode for a desired width and height. */
  141. /* Return the graphics mode or -1. */
  142. private int
  143. svga_find_mode(gx_device *dev, mode_info _ds *mip)
  144. {    for ( ; mip->mode >= 0; mip++ )
  145.        {    if ( mip->width == fb_dev->width &&
  146.              mip->height == fb_dev->height
  147.            )
  148.             return mip->mode;
  149.        }
  150.     return -1;
  151. }
  152.  
  153. /* Set the index for writing into the color DAC. */
  154. #define svga_dac_set_write_index(i) outportb(0x3c8, i)
  155.  
  156. /* Write 6-bit R,G,B values into the color DAC. */
  157. #define svga_dac_write(r, g, b)\
  158.   (outportb(0x3c9, r), outportb(0x3c9, g), outportb(0x3c9, b))
  159.  
  160. /* ------ Common procedures ------ */
  161.  
  162. /* Initialize the device structure and the DACs. */
  163. private int
  164. svga_open(gx_device *dev, int mode)
  165. {    fb_dev->raster = fb_dev->width;
  166.     fb_dev->x_pixels_per_inch =
  167.       fb_dev->y_pixels_per_inch =
  168.         fb_dev->height / 11.0;
  169.     /* Set the display mode. */
  170.     if ( svga_save_mode < 0 )
  171.         svga_save_mode = (*fb_dev->get_mode)();
  172.     (*fb_dev->set_mode)(mode);
  173.     /* Load the color DAC. */
  174.     svga_dac_set_write_index(0);
  175.        {    int c;
  176.         for ( c = 0; c < 64; c++ )
  177.            {    static byte c2[10] =
  178.                { 0, 42, 0, 0, 0, 0, 0, 0, 21, 63 };
  179.             svga_dac_write(c2[(c >> 2) & 9], c2[(c >> 1) & 9],
  180.                        c2[c & 9]);
  181.            }
  182.        }
  183.     /* Initialize the dynamic color table. */
  184.     next_color_index = first_color_index;
  185.     fb_dev->page = -1;
  186.     return 0;
  187. }
  188.  
  189. /* Close the device; reinitialize the display for text mode. */
  190. private int
  191. svga_close(gx_device *dev)
  192. {    if ( svga_save_mode >= 0 )
  193.         (*fb_dev->set_mode)(svga_save_mode);
  194.     svga_save_mode = -1;
  195.     return 0;
  196. }
  197.  
  198. /* Map a r-g-b color to a palette index. */
  199. /* The first 64 entries of the color map are set */
  200. /* for compatibility with the older display modes: */
  201. /* these are indexed as 0.0.R0.G0.B0.R1.G1.B1. */
  202. private gx_color_index
  203. svga_map_rgb_color(gx_device *dev, ushort r, ushort g, ushort b)
  204. {
  205. #define cv_bits(v,n) (v >> (gx_color_value_bits - n))
  206.     ushort r5 = cv_bits(r, 5), g5 = cv_bits(g, 5), b5 = cv_bits(b, 5);
  207.     static byte cube_bits[32] =
  208.        {    0, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  209.         8, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  210.         1, 128, 128, 128, 128, 128, 128, 128, 128, 128,
  211.         9
  212.        };
  213.     uint cx = ((uint)cube_bits[r5] << 2) + ((uint)cube_bits[g5] << 1) +
  214.           (uint)cube_bits[b5];
  215.     ushort rgb;
  216.     /* Check for a color on the cube. */
  217.     if ( cx < 64 ) return (gx_color_index)cx;
  218.     /* Not on the cube, check the dynamic color table. */
  219.     rgb = (r5 << 10) + (g5 << 5) + b5;
  220.        {    int i = next_color_index - first_color_index;
  221.         register ushort _ds *pdc = dynamic_colors + i;
  222.         while ( --i >= 0 )
  223.           if ( *--pdc == rgb )
  224.             return (gx_color_index)(i + first_color_index);
  225.        }
  226.     /* Not on the cube, and not in the dynamic table. */
  227.     /* Put in the dynamic table if space available. */
  228.     if ( next_color_index < 255 )
  229.        {    int i = next_color_index++;
  230.         dynamic_colors[i - first_color_index] = rgb;
  231.         svga_dac_set_write_index(i);
  232.         svga_dac_write(cv_bits(r, 6), cv_bits(g, 6), cv_bits(b, 6));
  233.         return (gx_color_index)i;
  234.        }
  235.     /* No space left, report failure. */
  236.     return gx_no_color_index;
  237. }
  238.  
  239. /* Map a color code to r-g-b. */
  240. /* This routine must invert the transformation of the one above. */
  241. /* Since this is practically never used, we just read the DAC. */
  242. private int
  243. svga_map_color_rgb(gx_device *dev, gx_color_index color, ushort prgb[3])
  244. {    uint cval;
  245.     outportb(0x3c7, (byte)color);
  246. #define dacin() (cval = inportb(0x3c9) >> 1,\
  247.   ((cval << 11) + (cval << 6) + (cval << 1) + (cval >> 4)) >>\
  248.    (16 - gx_color_value_bits))
  249.     prgb[0] = dacin();
  250.     prgb[1] = dacin();
  251.     prgb[2] = dacin();
  252. #undef dacin
  253.     return 0;
  254. }
  255.  
  256. /* Copy a monochrome bitmap.  The colors are given explicitly. */
  257. /* Color = gx_no_color_index means transparent (no effect on the image). */
  258. private int
  259. svga_copy_mono(gx_device *dev,
  260.   byte *base, int sourcex, int raster, gx_bitmap_id id,
  261.   int x, int y, int w, int h, gx_color_index czero, gx_color_index cone)
  262. {    register int xi;
  263.     uint skip = fb_dev->raster - w;
  264.     int yi;
  265.     fb_ptr ptr = (fb_ptr)0;
  266.     byte *srow = base + (sourcex >> 3);
  267.     uint imask = 0x80 >> (sourcex & 7);
  268.     validate_rect();
  269. #define izero (int)czero
  270. #define ione (int)cone
  271.     for ( yi = 0; yi < h; yi++ )
  272.        {    byte *sptr = srow;
  273.         uint bits = *sptr;
  274.         register uint mask = imask;
  275.         if ( PTR_OFF(ptr) <= skip )
  276.             set_pixel_write_ptr(ptr, fb_dev, x, y + yi);
  277.         for ( xi = 0; xi < w; xi++ )
  278.            {    if ( PTR_OFF(ptr) == 0 )
  279.                 set_pixel_write_ptr(ptr, fb_dev, x + xi, y + yi);
  280.             if ( bits & mask )
  281.                {    if ( ione != no_color ) *ptr = (byte)ione;
  282.                }
  283.             else
  284.                {    if ( izero != no_color ) *ptr = (byte)izero;
  285.                }
  286.             if ( !(mask >>= 1) ) mask = 0x80, bits = *++sptr;
  287.             ptr++;
  288.            }
  289.         ptr += skip;
  290.         srow += raster;
  291.        }
  292. #undef izero
  293. #undef ione
  294.     return 0;
  295. }
  296.  
  297. /* Copy a color pixelmap.  This is just like a bitmap, */
  298. /* except that each pixel takes 8 bits instead of 1. */
  299. private int
  300. svga_copy_color(gx_device *dev,
  301.   byte *base, int sourcex, int raster, gx_bitmap_id id,
  302.   int x, int y, int w, int h)
  303. {    int xi, yi;
  304.     int skip = raster - w;
  305.     byte *sptr = base + sourcex;
  306.     fb_ptr ptr;
  307.     validate_rect();
  308.     for ( yi = y; yi - y < h; yi++ )
  309.        {    ptr = 0;
  310.         for ( xi = x; xi - x < w; xi++ )
  311.            {    if ( PTR_OFF(ptr) == 0 )
  312.                 set_pixel_write_ptr(ptr, fb_dev, xi, yi);
  313.             *ptr++ = *sptr++;
  314.            }
  315.         sptr += skip;
  316.        }
  317.     return 0;
  318. }
  319.  
  320. /* Fill a rectangle. */
  321. private int
  322. svga_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  323.   gx_color_index color)
  324. {    uint raster = fb_dev->raster;
  325.     ushort limit = (ushort)-raster;
  326.     int yi;
  327.     fb_ptr ptr;
  328.     if ( x < 0 || x + w > dev->width ) return -1;    /* skip w */
  329.     set_pixel_write_ptr(ptr, fb_dev, x, y);
  330.     /* Most fills are very small and don't cross a page boundary. */
  331.     yi = h;
  332.     switch ( w )
  333.        {
  334.     case 0: return 0;        /* no-op */
  335.     case 1:
  336.         while ( --yi >= 0 && PTR_OFF(ptr) < limit )
  337.             ptr[0] = (byte)color,
  338.             ptr += raster;
  339.         if ( !++yi ) return 0;
  340.         break;
  341.     case 2:
  342.         while ( --yi >= 0 && PTR_OFF(ptr) < limit )
  343.             ptr[0] = ptr[1] = (byte)color,
  344.             ptr += raster;
  345.         if ( !++yi ) return 0;
  346.         break;
  347.     case 3:
  348.         while ( --yi >= 0 && PTR_OFF(ptr) < limit )
  349.             ptr[0] = ptr[1] = ptr[2] = (byte)color,
  350.             ptr += raster;
  351.         if ( !++yi ) return 0;
  352.         break;
  353.     case 4:
  354.         while ( --yi >= 0 && PTR_OFF(ptr) < limit )
  355.             ptr[0] = ptr[1] = ptr[2] = ptr[3] = (byte)color,
  356.             ptr += raster;
  357.         if ( !++yi ) return 0;
  358.         break;
  359.     default:
  360.         if ( w < 0 ) return 0;
  361.        }
  362.     while ( --yi >= 0 )
  363.        {    if ( PTR_OFF(ptr) < limit )
  364.            {    memset(ptr, (byte)color, w);
  365.             ptr += raster;
  366.            }
  367.         else if ( PTR_OFF(ptr) <= (ushort)(-w) )
  368.            {    memset(ptr, (byte)color, w);
  369.             if ( yi > 0 )
  370.                 set_pixel_write_ptr(ptr, fb_dev, x, y + h - yi);
  371.            }
  372.         else
  373.            {    uint left = -PTR_OFF(ptr);
  374.             memset(ptr, (byte)color, left);
  375.             set_pixel_write_ptr(ptr, fb_dev, x + left, y + h - 1 - yi);
  376.             memset(ptr, (byte)color, w - left);
  377.             ptr += raster - left;
  378.            }
  379.        }
  380.     return 0;
  381. }
  382.  
  383. /* Read scan lines back from the frame buffer. */
  384. int
  385. svga_get_bits(gx_device *dev, int y, byte *data, uint size, int pad_to_word)
  386. {    /* We don't have to worry about padding, because we read back */
  387.     /* a byte per pixel and the frame buffer width is always */
  388.     /* a multiple of 8 pixels. */
  389.     uint bytes_per_row = dev->width;
  390.     uint count = min(dev->height - y, size / bytes_per_row);
  391.     byte *dest = data;
  392.     ushort limit = (ushort)-bytes_per_row;
  393.     int j;
  394.     for ( j = count; j--; dest += bytes_per_row, y++ )
  395.        {    fb_ptr src;
  396.         set_pixel_read_ptr(src, fb_dev, 0, y);
  397.         /* The logic here is similar to fill_rectangle. */
  398.         if ( PTR_OFF(src) <= limit )
  399.             memcpy(dest, src, bytes_per_row);
  400.         else
  401.            {    uint left = -PTR_OFF(src);
  402.             memcpy(dest, src, left);
  403.             set_pixel_read_ptr(src, fb_dev, left, y);
  404.             memcpy(dest + left, src, bytes_per_row - left);
  405.            }
  406.        }
  407.     return count;
  408. }
  409.  
  410. /* ------ The VESA device ------ */
  411.  
  412. private dev_proc_open_device(vesa_open);
  413. private gx_device_procs vesa_procs = svga_procs(vesa_open);
  414. private int vesa_get_mode(P0());
  415. private void vesa_set_mode(P1(int));
  416. private void vesa_set_page(P3(gx_device_svga *, int, int));
  417. gx_device_svga gs_vesa_device =
  418.     svga_device(vesa_procs, "vesa", vesa_get_mode, vesa_set_mode, vesa_set_page);
  419.  
  420. /* Define the structure for information returned by the BIOS. */
  421. #define bits_include(a, m) !(~(a) & (m))
  422. typedef enum {
  423.     m_supported = 1,
  424.     m_graphics = 0x10
  425. } mode_attribute;
  426. typedef enum {
  427.     w_supported = 1,
  428.     w_readable = 2,
  429.     w_writable = 4
  430. } win_attribute;
  431. typedef struct {
  432.     ushort mode_attributes;
  433.     byte win_a_attributes;
  434.     byte win_b_attributes;
  435.     ushort win_granularity;
  436.     ushort win_size;
  437.     ushort win_a_segment;
  438.     ushort win_b_segment;
  439.     void (*win_func_ptr)(P2(int, int));
  440.     ushort bytes_per_line;
  441.         /* Optional information */
  442.     ushort x_resolution;
  443.     ushort y_resolution;
  444.     byte x_char_size;
  445.     byte y_char_size;
  446.     byte number_of_planes;
  447.     byte bits_per_pixel;
  448.     byte number_of_banks;
  449.     byte memory_model;
  450.     byte bank_size;
  451. } vesa_info;
  452.  
  453. /* Read the device mode */
  454. private int
  455. vesa_get_mode()
  456. {    registers regs;
  457.     regs.h.ah = 0x4f;
  458.     regs.h.al = 0x03;
  459.     int86(0x10, ®s, ®s);
  460.     return regs.rshort.bx;
  461. }
  462.  
  463. /* Set the device mode */
  464. private void
  465. vesa_set_mode(int mode)
  466. {    registers regs;
  467.     regs.h.ah = 0x4f;
  468.     regs.h.al = 0x02;
  469.     regs.rshort.bx = mode;
  470.     int86(0x10, ®s, ®s);
  471. }
  472.  
  473. /* Read information about a device mode */
  474. private int
  475. vesa_get_info(int mode, vesa_info _ss *info)
  476. {    registers regs;
  477.     struct SREGS sregs;
  478.     regs.h.ah = 0x4f;
  479.     regs.h.al = 0x01;
  480.     regs.rshort.cx = mode;
  481.     regs.rshort.di = PTR_OFF(info);
  482.     segread(&sregs);
  483.     sregs.es = sregs.ss;
  484.     int86x(0x10, ®s, ®s, &sregs);
  485. #ifdef DEBUG
  486.     if ( regs.h.ah == 0 && regs.h.al == 0x4f )
  487.         dprintf8("vesa_get_info(%x): ma=%x wa=%x/%x wg=%x ws=%x wseg=%x/%x\n",
  488.              mode, info->mode_attributes,
  489.              info->win_a_attributes, info->win_b_attributes,
  490.              info->win_granularity, info->win_size,
  491.              info->win_a_segment, info->win_b_segment);
  492.     else
  493.         dprintf3("vesa_get_info(%x) failed: ah=%x al=%x\n",
  494.              mode, regs.h.ah, regs.h.al);
  495. #endif
  496.     return (regs.h.ah == 0 && regs.h.al == 0x4f ? 0 : -1);
  497. }
  498.  
  499. /* Initialize the graphics mode. */
  500. private int
  501. vesa_open(gx_device *dev)
  502. {    /* Select the proper video mode */
  503.        {    vesa_info info;
  504.         static mode_info mode_table[6] = {
  505.            {     640,  400, 0x100    },
  506.            {     640,  480, 0x101    },
  507.            {     800,  600, 0x103    },
  508.            {    1024,  768, 0x105    },
  509.            {    1280, 1024, 0x107    },
  510.            {    -1, -1, -1    }
  511.         };
  512.         mode_info _ds *mip;
  513.         for ( mip = mode_table; mip->mode >= 0; mip++ )
  514.            {    if ( mip->width == fb_dev->width &&
  515.                  mip->height == fb_dev->height &&
  516.                  vesa_get_info(mip->mode, &info) >= 0 &&
  517.                  bits_include(info.mode_attributes,
  518.                 m_supported | m_graphics) &&
  519.                  info.win_granularity == 64 &&
  520.                  info.win_size == 64 &&
  521.                  bits_include(info.win_a_attributes,
  522.                 w_supported) &&
  523.                  info.win_a_segment == regen
  524.                )
  525.                {    /* Make sure we can both read & write. */
  526.                 /* Initialize for the default case. */
  527.                 fb_dev->wnum_read = 0;
  528.                 fb_dev->wnum_write = 0;
  529.                 if ( bits_include(info.win_a_attributes,
  530.                     w_readable | w_writable)
  531.                    )
  532.                     break;
  533.                 else if ( info.win_b_segment == regen &&
  534.                     bits_include(info.win_b_attributes,
  535.                         w_supported) &&
  536.                     bits_include(info.win_a_attributes |
  537.                         info.win_b_attributes,
  538.                         w_readable | w_writable)
  539.                    )
  540.                    {    /* Two superimposed windows. */
  541.                     if ( !bits_include(info.win_a_attributes,
  542.                         w_writable)
  543.                        )
  544.                         fb_dev->wnum_write = 1;
  545.                     else
  546.                         fb_dev->wnum_read = 1;
  547.                    }
  548.                 break;
  549.                }
  550.            }
  551.         if ( mip->mode < 0 ) return -1;    /* mode not available */
  552.         fb_dev->info.vesa.bios_set_page = info.win_func_ptr;
  553.         return svga_open(dev, mip->mode);
  554.        }
  555. }
  556.  
  557. /* Set the current display page. */
  558. private void
  559. vesa_set_page(gx_device_svga *dev, int pn, int wnum)
  560. {
  561. #if USE_ASM
  562. extern void vesa_call_set_page(P3(void (*)(P2(int, int)), int, int));
  563.     if ( dev->info.vesa.bios_set_page != NULL )
  564.         vesa_call_set_page(dev->info.vesa.bios_set_page, pn, wnum);
  565.     else
  566. #endif
  567.        {    registers regs;
  568.         regs.rshort.dx = pn;
  569.         regs.h.ah = 0x4f;
  570.         regs.h.al = 5;
  571.         regs.rshort.bx = wnum;
  572.         int86(0x10, ®s, ®s);
  573.        }
  574. }
  575.  
  576. /* ------ The ATI Wonder device ------ */
  577.  
  578. private dev_proc_open_device(atiw_open);
  579. private gx_device_procs atiw_procs = svga_procs(atiw_open);
  580. private int atiw_get_mode(P0());
  581. private void atiw_set_mode(P1(int));
  582. private void atiw_set_page(P3(gx_device_svga *, int, int));
  583. gx_device_svga gs_atiw_device =
  584.     svga_device(atiw_procs, "atiw", atiw_get_mode, atiw_set_mode, atiw_set_page);
  585.  
  586. /* Read the device mode */
  587. private int
  588. atiw_get_mode()
  589. {    registers regs;
  590.     regs.h.ah = 0xf;
  591.     int86(0x10, ®s, ®s);
  592.     return regs.h.al;
  593. }
  594.  
  595. /* Set the device mode */
  596. private void
  597. atiw_set_mode(int mode)
  598. {    registers regs;
  599.     regs.h.ah = 0;
  600.     regs.h.al = mode;
  601.     int86(0x10, ®s, ®s);
  602. }
  603.  
  604. /* Initialize the graphics mode. */
  605. private int
  606. atiw_open(gx_device *dev)
  607. {    /* Select the proper video mode */
  608.        {    static mode_info mode_table[4] = {
  609.            {     640,  400, 0x61    },
  610.            {     640,  480, 0x62    },
  611.            {     800,  600, 0x63    },
  612.            {    -1, -1, -1    }
  613.         };
  614.         int mode = svga_find_mode(dev, mode_table);
  615.         if ( mode < 0 ) return -1;    /* mode not available */
  616.         fb_dev->info.atiw.select_reg = *(int *)MK_PTR(0xc000, 0x10);
  617.         return svga_open(dev, mode);
  618.        }
  619. }
  620.  
  621. /* Set the current display page. */
  622. private void
  623. atiw_set_page(gx_device_svga *dev, int pn, int wnum)
  624. {    int select_reg = dev->info.atiw.select_reg;
  625.     byte reg;
  626.     disable();
  627.     outportb(select_reg, 0xb2);
  628.     reg = inportb(select_reg + 1);
  629.     outportb(select_reg, 0xb2);
  630.     outportb(select_reg + 1, (reg & 0xe1) + (pn << 1));
  631.     enable();
  632. }
  633.  
  634. /* ------ The Tseng Labs ET3000/4000 device ------ */
  635.  
  636. private dev_proc_open_device(tseng_open);
  637. private gx_device_procs tseng_procs = svga_procs(tseng_open);
  638. /* We can use the tseng_get/set_mode procedures. */
  639. private void tseng_set_page(P3(gx_device_svga *, int, int));
  640. gx_device_svga gs_tseng_device =
  641.     svga_device(tseng_procs, "tseng", atiw_get_mode, atiw_set_mode, tseng_set_page);
  642.  
  643. /* Initialize the graphics mode. */
  644. private int
  645. tseng_open(gx_device *dev)
  646. {    fb_dev->wnum_read = 1;
  647.     fb_dev->wnum_write = 0;
  648.     /* Select the proper video mode */
  649.        {    static mode_info mode_table[5] = {
  650.            {     640,  350, 0x2d    },
  651.            {     640,  480, 0x2e    },
  652.            {     800,  600, 0x30    },
  653.            {     1024, 768, 0x38    },
  654.            {    -1, -1, -1    }
  655.         };
  656.         int mode = svga_find_mode(dev, mode_table);
  657.         if ( mode < 0 ) return -1;    /* mode not available */
  658.         return svga_open(dev, mode);
  659.        }
  660. }
  661.  
  662. /* Set the current display page. */
  663. private void
  664. tseng_set_page(gx_device_svga *dev, int pn, int wnum)
  665. {    /****** WE DON'T KNOW HOW TO SET THE READ PAGE ******/
  666.     outportb(0x3cd, 0x40 + pn);
  667. }
  668.